use super::*;
use crate::drawer::write_png;

#[test]
fn test_route_single_target_path() {
    let mut grid = Grid::new(100, 100);
    match route_single_target_path(
        &mut grid,
        Coord::new(10, 10),
        Coord::new(80, 80),
        Color::white(),
    ) {
        Ok(path) => println!("Route success with len: {}", path.len()),
        Err(err) => println!("{}", err),
    }
    write_png(&grid, "./pic/test_route_single_target_path.png");
}

#[test]
fn test_route_single_target_path_with_astar() {
    let mut grid = Grid::new(100, 100);
    match route_single_target_path_with_astar(
        &mut grid,
        Coord::new(10, 10),
        Coord::new(80, 80),
        Color::white(),
    ) {
        Ok(path) => println!("Route success with len: {}", path.len()),
        Err(err) => println!("{}", err),
    }
    write_png(&grid, "./pic/test_route_single_target_path_with_astar.png");
}

#[test]
fn test_route_single_target_path_mutil_times() {
    let mut grid = Grid::new(100, 100);
    assert!(route_single_target_path(
        &mut grid,
        Coord::new(10, 10),
        Coord::new(80, 80),
        Color::white()
    )
    .is_ok());
    assert!(route_single_target_path(
        &mut grid,
        Coord::new(10, 90),
        Coord::new(70, 8),
        Color::red()
    )
    .is_ok());

    write_png(&grid, "./pic/test_route_single_target_path_mutil_times.png");
}

#[test]
fn test_route_multi_targets_path() {
    let mut grid = Grid::new(100, 100);
    assert!(route_multi_targets_path(
        &mut grid,
        Coord::new(10, 10),
        &[
            Coord::new(10, 80),
            Coord::new(11, 10),
            Coord::new(81, 2),
            Coord::new(71, 40)
        ],
        Color::white()
    )
    .is_ok());
    write_png(&grid, "./pic/test_route_multi_targets_path.png");
}

#[test]
fn test_route_multi_targets_path_with_astar() {
    let mut grid = Grid::new(100, 100);
    assert!(route_multi_targets_path_with_astar(
        &mut grid,
        Coord::new(10, 10),
        &[
            Coord::new(10, 80),
            Coord::new(11, 10),
            Coord::new(81, 2),
            Coord::new(71, 40)
        ],
        Color::white()
    )
    .is_ok());
    write_png(&grid, "./pic/test_route_multi_targets_path_with_astar.png");
}

#[test]
fn test_route_multi_targets_path_multi_time() {
    let mut grid = Grid::new(100, 100);
    assert!(route_multi_targets_path(
        &mut grid,
        Coord::new(10, 10),
        &[
            Coord::new(10, 80),
            Coord::new(11, 10),
            Coord::new(81, 2),
            Coord::new(71, 40)
        ],
        Color::white()
    )
    .is_ok());
    assert!(route_multi_targets_path(
        &mut grid,
        Coord::new(10, 90),
        &[Coord::new(40, 44), Coord::new(20, 76),],
        Color::red()
    )
    .is_ok());

    write_png(&grid, "./pic/test_route_multi_targets_path_multi_time.png");
}

#[test]
fn useful() {
    let s = Coord::new(1, 5);
    let e = Coord::new(12, 13);

    let mut grid = Grid::new(15, 15);
    match route_single_target_path(&mut grid, s, e, Color::green()) {
        Ok(path) => println!("Route success with len: {}", path.len()),
        Err(err) => println!("{}", err),
    }
    write_png(&grid, "./pic/t1.png");

    let mut grid = Grid::new(15, 15);
    match route_single_target_path_with_len(&mut grid, s, e, 23, Color::green()) {
        Ok(path) => println!("Route success with len: {}", path.len()),
        Err(msg) => println!("{}", msg),
    }
    write_png(&grid, "./pic/t2.png");
}

#[test]
fn eda_test() {
    let mut grid = Grid::new(150, 150);

    let bus_nets = [
        (Coord::new(30, 64), Coord::new(140, 71)),
        (Coord::new(20, 84), Coord::new(120, 105)),
        (Coord::new(18, 11), Coord::new(140, 22)),
        (Coord::new(20, 44), Coord::new(130, 65)),
    ];
    let nets = [
        (
            Coord::new(10, 10),
            vec![Coord::new(5, 140), Coord::new(70, 120), Coord::new(30, 15)],
        ),
        (
            Coord::new(100, 50),
            vec![Coord::new(142, 30), Coord::new(130, 100)],
        ),
    ];

    let bus_len = bus_nets
        .iter()
        .map(|(s, e)| s.manhattan_distance(e))
        .max()
        .unwrap() as usize
        + 14;

    for (i, net) in bus_nets[..2].iter().enumerate() {
        if let Err(err) = route_single_target_path_with_len(
            &mut grid,
            net.0,
            net.1,
            bus_len,
            Color::rgb((i * 60) as u8, 100, 100),
        ) {
            println!("When route net '{net:?}': {err}");
            return;
        }
    }

    for (i, net) in nets.iter().enumerate() {
        if let Err(err) = route_multi_targets_path(
            &mut grid,
            net.0,
            &net.1,
            Color::rgb(100, (i * 60) as u8, 100),
        ) {
            println!("When route net '{net:?}': {err}");
            return;
        }
    }

    for (i, net) in bus_nets[2..].iter().enumerate() {
        if let Err(err) = route_single_target_path_with_len(
            &mut grid,
            net.0,
            net.1,
            bus_len,
            Color::rgb(((i + 2) * 60) as u8, 100, 100),
        ) {
            println!("When route net '{net:?}': {err}");
            return;
        }
    }

    write_png(&grid, "./pic/eda_test.png");
}
